package com.btshop.goods.controller;
import com.btshop.goods.pojo.Template;
import com.btshop.goods.service.TemplateService;
import com.github.pagehelper.PageInfo;
import com.util.Result;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @ClassName: TemplateController
 * @description: Template 控制层接口定义类
 * @author: 李东
 * @Date: 2020-7-20
 */
@Api(value = "TemplateController")
@Slf4j
@RestController
@RequestMapping("/template")
@CrossOrigin
public class TemplateController {

    @Autowired
    private TemplateService templateService;

    /**
     * 根据分类id查询模板
     * @param categoryId
     * @return
     */
    @ApiOperation(value = "Template根据分类id查询模板",notes = "根据分类id查询模板",tags = {"TemplateController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "categoryId", value = "分类id", required = true, dataType = "Long")
    })
    @GetMapping(value = "/category/{categoryId}" )
    public Result<Template> findByCategoryId(@PathVariable("categoryId")Long categoryId){
        Template template = templateService.findByCategoryId(categoryId);
        return Result.buildSuccess("根据分类id查询模板成功！",template);
    }

    /***
     * Template分页条件搜索实现
     * @param template
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "Template条件分页查询",notes = "分页条件查询Template方法详情",tags = {"TemplateController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "Template对象",value = "传入JSON数据",required = false) Template template, @PathVariable  int page, @PathVariable  int size){
        log.info("TemplateController findPage params , template : {} , page : {} , size : {}",template,page,size);
        //调用TemplateService实现分页条件查询Template
        PageInfo<Template> pageInfo = templateService.findPage(template, page, size);
        return Result.buildSuccess("查询成功！", pageInfo);
    }

    /***
     * Template分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "Template分页查询",notes = "分页查询Template方法详情",tags = {"TemplateController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        log.info("TemplateController findPage params , page : {} , size : {}",page,size);
        //调用TemplateService实现分页查询Template
        PageInfo<Template> pageInfo = templateService.findPage(page, size);
        return Result.buildSuccess("查询成功！", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param template
     * @return
     */
    @ApiOperation(value = "Template条件查询",notes = "条件查询Template方法详情",tags = {"TemplateController"})
    @PostMapping(value = "/search" )
    public Result<List<Template>> findList(@RequestBody(required = false) @ApiParam(name = "Template对象",value = "传入JSON数据",required = false) Template template){
        log.info("TemplateController findList params , template : {} ",template);
        //调用TemplateService实现条件查询Template
        List<Template> list = templateService.findList(template);
        return Result.buildSuccess("查询成功！", list);
    }

    /***
     * 根据主键删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Template根据主键id删除",notes = "根据主键删除Template方法详情",tags = {"TemplateController"})
    @ApiImplicitParams({
                @ApiImplicitParam(paramType = "path", name ="id", value = "主键", required = true, dataType = "Long")
    })
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable("id") Long id){
        log.info("TemplateController delete params ,   id : {}   ",id);
        //调用TemplateService实现根据主键删除
        templateService.delete(id);
        return Result.buildSuccess("删除成功！");
    }

    /***
     * 修改Template数据
     * @param template
     * @param id
     * @return
     */
    @ApiOperation(value = "Template根据id修改",notes = "根据ID修改Template方法详情",tags = {"TemplateController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name ="id", value = "主键", required = true, dataType = "Long")
     })
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @ApiParam(name = "Template对象",value = "传入JSON数据",required = false) Template template,@PathVariable("id") Long id){
        log.info("TemplateController update params ,  template : {},  id : {}   ",template,id);
        //设置主键值
        template.setId(id);
        //调用TemplateService实现修改Template
        templateService.update(template);
        return Result.buildSuccess("修改成功！");
    }

    /***
     * 新增Template数据
     * @param template
     * @return
     */
    @ApiOperation(value = "Template添加",notes = "添加Template方法详情",tags = {"TemplateController"})
    @PostMapping
    public Result add(@RequestBody  @ApiParam(name = "Template对象",value = "传入JSON数据",required = true) Template template){
        log.info("TemplateController add params , template : {}",template);
        //调用TemplateService实现添加Template
        templateService.add(template);
        return Result.buildSuccess("添加成功！");
    }

    /***
     * 根据ID查询Template数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Template根据id查询",notes = "根据ID查询Template方法详情",tags = {"TemplateController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name ="id", value = "主键", required = true, dataType = "Long")
            })
    @GetMapping("/{id}")
    public Result<Template> findById(@PathVariable("id") Long id){
        log.info("TemplateController findById params ,   id : {}   ",id);
        //调用TemplateService实现根据主键查询Template
        Template template = templateService.findById(id);
        return Result.buildSuccess("查询成功！",template);
    }

    /***
     * 查询Template全部数据
     * @return
     */
    @ApiOperation(value = "查询所有Template",notes = "查询所Template有方法详情",tags = {"TemplateController"})
    @GetMapping
    public Result<List<Template>> findAll(){
        //调用TemplateService实现查询所有Template
        List<Template> list = templateService.findAll();
        return Result.buildSuccess("查询成功！",list);
    }
}
